package com.pub.paypal.controller;

import com.paypal.base.rest.PayPalRESTException;
import com.pub.paypal.common.operation.CaptureOrder;
import com.pub.paypal.common.operation.CreateOrder;
import com.pub.paypal.common.operation.GetOrder;
import com.pub.paypal.common.response.CommonResult;
import com.pub.paypal.model.CreateOrderEntity;
import com.pub.paypal.model.RefundEntity;
import com.pub.paypal.service.PaypalService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * paypal支付前端控制器
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/paypal")
public class PaypalController  {

    private PaypalService paypalService;

    private CaptureOrder captureOrder;

    private CreateOrder createOrder;

    private GetOrder getOrder;

    /**
     * 创建订单
     * @return
     * @throws PayPalRESTException
     */
    @PostMapping(value ="/createOrder" )
    public String createOrder(@Validated @RequestBody CreateOrderEntity createOrderEntity){
        String payUrl = "";
        try {
            payUrl = createOrder.createOrder(createOrderEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return payUrl;
    }

    /**
     * 查询订单详情
     * @return
     * @throws PayPalRESTException
     */
    @GetMapping(value ="/getOrder" )
    public String getOrder(String orderId){
        String orderString = "";
        try {
            orderString = getOrder.get(orderId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orderString;
    }

    /**
     * 查询扣款
     * @param captureId 扣款成功后返回的扣款信息id
     * @return
     * @throws PayPalRESTException
     */
    @GetMapping(value ="/getCapture" )
    public CommonResult<String> getCapture(String captureId){
        String captureString = null;
        try {
            captureString = captureOrder.getCapture(captureId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.success(captureString);
    }

    /**
     * 退款操作
     * @param refundEntity 用户退款对象
     * @return
     * @throws PayPalRESTException
     */
    @PostMapping(value ="/refund" )
    public CommonResult<String> refund(@RequestBody @Validated RefundEntity refundEntity){
        String captureString = null;
        try {
            captureString = paypalService.refund(refundEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.success(captureString);
    }

    /**
     * 查询退款信息
     * @param refundId 退款id 退款的时候生成的
     * @return
     * @throws PayPalRESTException
     */
    @GetMapping(value ="/getRefund" )
    public CommonResult<String> refund(String refundId){
        String refundInfoString = null;
        try {
            refundInfoString = paypalService.getRefund(refundId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.success(refundInfoString);
    }
}